package com.by.controller;

import com.by.bean.Dictionaries;
import com.by.common.bean.ResultBean;
import com.by.service.DictionariesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author HY
 * @version 1.0
 * @description
 * @date 2025/2/21
 * @since JDK21
 */

@RestController
@Api(tags = "字典表")
@RequestMapping("/api/dictionaries")
public class DictionariesController {


    @Autowired
    DictionariesService dictionariesService;

    @PostMapping("/save")
    @ApiOperation("保存字典表")
    public ResultBean save(@RequestBody Dictionaries dictionaries){
        int save = dictionariesService.save(dictionaries);
        return save!=1 ? new ResultBean(0,"保存失败",null) : new ResultBean(200,"保存成功",null);
    }

    @GetMapping("/query/{pid}")
    @ApiOperation("查询字典表")
    public  ResultBean<List<Dictionaries>> query(@PathVariable("pid") int pid){
        List<Map> query = dictionariesService.query(pid);
        return query.size()!=0 ? new ResultBean(200,"查询成功",query) : new ResultBean(0,"查询失败",null);
    }


    @PutMapping("/update")
    @ApiOperation("修改字典表")
    public  ResultBean update(@RequestBody Dictionaries dictionaries){
        int update = dictionariesService.update(dictionaries);
        return update!=1 ? new ResultBean(0,"修改失败",null) : new ResultBean(200,"修改成功",null);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除字典表")
    public ResultBean delete(@PathVariable("id") int id){
        int delete = dictionariesService.delete(id);
        return delete!=1 ? new ResultBean(0,"删除失败",null) : new ResultBean(200,"删除成功",null);
    }

}
